package org.ruanjj.addtwonumbers;


import org.ruanjj.linkedlist.ListNode;

/*
* 输入：(7 -> 1 -> 6) + (5 -> 9 -> 2)，即617 + 295
  输出：2 -> 1 -> 9，即912
* */
public class Solution2 {
    int count = 0;
    public ListNode addTwoNumbers(ListNode l1, ListNode l2){
        ListNode cur = new ListNode(-1);
        if(l1 == null && l2 == null){
            return count > 0 ? new ListNode(count):null;
        }
        int val1 = l1 == null? 0:l1.val;
        int val2 = l2 == null? 0:l2.val;

        int val = count + val1 + val2;
        count = val%10 >= 0? val/10:0;
        cur.next = new ListNode(val%10);
        cur  = cur.next;
        if(l1 == null){
            cur.next = addTwoNumbers(null,l2.next);
        }else if(l2 == null){
            cur.next = addTwoNumbers(l1.next,null);
        }else{
            cur.next = addTwoNumbers(l1.next,l2.next);
        }
        return cur;
    }
}
